Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Added missing batch/agg functions #847

Merged
merged 4 commits into from
Feb 26, 2018

Conversation

Aergonus
Copy link
Contributor

@Aergonus Aergonus commented Feb 8, 2018

This is also in support of the summarize function #837

Basically ported over https://github.com/grafana/metrictank/blob/master/expr/seriesaggregators.go

Also removed panics since they all return math.NaN

median
def safeMedian(values):
  safeValues = [v for v in values if v is not None]
  if safeValues:
    sortedVals = sorted(safeValues)
    mid = len(sortedVals) // 2
    if len(sortedVals) % 2 == 0:
      return float(sortedVals[mid-1] + sortedVals[mid]) / 2
    else:
      return sortedVals[mid]

diff
def safeDiff(values):
  safeValues = [v for v in values if v is not None]
  if safeValues:
    values = list(map(lambda x: x*-1, safeValues[1:]))
    values.insert(0, safeValues[0])
    return sum(values)

stddev
def safeStdDev(a):
  sm = safeSum(a)
  ln = safeLen(a)
  avg = safeDiv(sm,ln)
  if avg is None: return None
  sum = 0
  safeValues = [v for v in a if v is not None]
  for val in safeValues:
     sum = sum + (val - avg) * (val - avg)
  return math.sqrt(sum/ln)

range
  'range': lambda row: safeSubtract(safeMax(row), safeMin(row)),

multiply
  'multiply': lambda row: safeMul(*row),
def safeMul(*factors):
  if None in factors:
    return None

  factors = [float(x) for x in factors]
  product = reduce(lambda x,y: x*y, factors)
  return product

If 0 length is passed in, we should return math.NaN like python code
Also follow SEP (Somebody else's problem)
Basically ported over https://github.com/grafana/metrictank/blob/master/expr/seriesaggregators.go
```
median
def safeMedian(values):
  safeValues = [v for v in values if v is not None]
  if safeValues:
    sortedVals = sorted(safeValues)
    mid = len(sortedVals) // 2
    if len(sortedVals) % 2 == 0:
      return float(sortedVals[mid-1] + sortedVals[mid]) / 2
    else:
      return sortedVals[mid]

diff
def safeDiff(values):
  safeValues = [v for v in values if v is not None]
  if safeValues:
    values = list(map(lambda x: x*-1, safeValues[1:]))
    values.insert(0, safeValues[0])
    return sum(values)

stddev
def safeStdDev(a):
  sm = safeSum(a)
  ln = safeLen(a)
  avg = safeDiv(sm,ln)
  if avg is None: return None
  sum = 0
  safeValues = [v for v in a if v is not None]
  for val in safeValues:
     sum = sum + (val - avg) * (val - avg)
  return math.sqrt(sum/ln)

range
  'range': lambda row: safeSubtract(safeMax(row), safeMin(row)),

multiply
  'multiply': lambda row: safeMul(*row),
def safeMul(*factors):
  if None in factors:
    return None

  factors = [float(x) for x in factors]
  product = reduce(lambda x,y: x*y, factors)
  return product
```
Note that this fixes bug --> unable to get cnt aggfunc
Also ignores archive which seems to be different
func Mult(in []schema.Point) float64 {
valid := false
mult := float64(1)
for _, term := range in {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

term is the terminology for sum, when it's multiplication they're called factors.

for _, term := range in {
if math.IsNaN(term.Val) {
// NaN * anything equals NaN()
mult = math.NaN()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just return term.Val here and skip all the other stuff

avg := Avg(in)
if !math.IsNaN(avg) {
num := float64(0)
totalDeviationSquared := float64(0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

confusing name. it's not one total deviation that is squared. it's the sum of squares of deviations.


func StdDev(in []schema.Point) float64 {
avg := Avg(in)
if !math.IsNaN(avg) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could turn this into an early return and reduce the needed indentation

func Range(in []schema.Point) float64 {
min := Min(in)
max := Max(in)
return max - min
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can speed this function up by only looping once

@Dieterbe
Copy link
Contributor

LGTM. thanks @Spellchaser

@Dieterbe Dieterbe merged commit 012f160 into grafana:master Feb 26, 2018
@Aergonus Aergonus deleted the feature/batchAggs branch February 26, 2018 15:19
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants